package cn.pengpeng.day04.top2;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.TreeSet;

import org.apache.commons.io.FileUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.codehaus.jackson.map.ObjectMapper;


public class TopN2MR {
	public static class MapTask extends Mapper<LongWritable, Text, Text, MovieBean>{
		@Override
		protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, MovieBean>.Context context)
				throws IOException, InterruptedException {
			ObjectMapper mapper = new ObjectMapper();
			MovieBean movie = mapper.readValue(value.toString(), MovieBean.class);
			context.write(new Text(movie.getUid()), movie);
		}
	}
	
	public static class ReduceTask extends Reducer<Text, MovieBean, MovieBean, NullWritable>{
		@Override
		protected void reduce(Text key, Iterable<MovieBean> values,
				Reducer<Text, MovieBean, MovieBean, NullWritable>.Context context)
				throws IOException, InterruptedException {
			
			TreeSet<MovieBean> tree = new TreeSet<>(new Comparator<MovieBean>() {

				@Override
				public int compare(MovieBean o1, MovieBean o2) {
					
					if(o2.getRate()-o1.getRate()==0){
						return -(o2.getMovie().compareTo(o1.getMovie()));
					}else{
						return -(o2.getRate()-o1.getRate());
					}
				}
			});
			for (MovieBean movieBean : values) {
				MovieBean movieBean2 = new MovieBean();
				movieBean2.set(movieBean.getMovie(), movieBean.getRate(), movieBean.getTimeStamp(), movieBean.getUid());
				if(tree.size()<20){
					tree.add(movieBean2);
				}else{
					MovieBean first = tree.first();
					if(first.getRate()<movieBean.getRate()){
						tree.remove(first);
						tree.add(movieBean2);
					}
					
				}
			}
			
			for (MovieBean movieBean : tree) {
				context.write(movieBean, NullWritable.get());
			}
			
			
		}
	}
	
	public static void main(String[] args) throws Exception{
		Configuration conf = new Configuration();
		Job job = Job.getInstance(conf);
		
		//设置job的map 和 reduce 是哪一个 提交任务的类
		job.setMapperClass(MapTask.class);
		job.setReducerClass(ReduceTask.class);
		job.setJarByClass(TopN2MR.class);
		
		//设置输出的类型
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(MovieBean.class);
		
		job.setOutputKeyClass(MovieBean.class);
		job.setOutputValueClass(NullWritable.class);
		
		job.setNumReduceTasks(2);
		//设置输入目录和输出目录
		FileInputFormat.addInputPath(job, new Path("d:/data/rating.json"));
		FileOutputFormat.setOutputPath(job, new Path("d:/out/topn2"));
		
		
		File file = new File("d:/out/topn2");
		if(file.exists()){
			FileUtils.deleteDirectory(file);
		}
		
		//提交任务并等待任务结束，能够打印日志
		boolean waitForCompletion = job.waitForCompletion(true);
		System.out.println(waitForCompletion?"程序正常退出":"程序有问题了");
	}

}
